package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.VisitorService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service // dubbo的
public class VisitorServiceImpl implements VisitorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Visitor> findByUserIdAndLastAccessTime(Long userId, String lastAccessTime) {
        // 1.组装查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId) // 登录人id
                .and("date").gt(Long.valueOf(lastAccessTime)) // 大于上次登录时间
        ).with(Sort.by(Sort.Order.desc("date"))) // 时间降序
                .skip(0).limit(5); // 最多显示前五条

        // 2.查询
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);

        // 3.返回
        return visitorList;
    }
}
